package com.stylefeng.guns.modular.netcar.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.Tools.CodeMsg;
import com.stylefeng.guns.core.common.Tools.Result;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.validater.Insert;
import com.stylefeng.guns.modular.netcar.model.AppVersion;
import com.stylefeng.guns.modular.netcar.service.IAppVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * APP版本设置
 *
 * @author sasazaixian
 * @Date 2018-10-23 11:02
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/appVersion"})
@Api(value = "APP版本设置操作相关api", tags = {"APP版本设置操作接口"})
@Slf4j(topic = "App版本设置")
public class AppVersionController extends BaseController {
    @Autowired
    private IAppVersionService appVersionService;

    /**
     * 通过主键获取APP版本设置对象
     * @return
     */
    @ApiOperation(value = "通过主键获取APP版本设置对象", notes = "通过主键获取APP版本设置对象", httpMethod = "GET")
    @GetMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result getById() {
        try {
            return Result.success( appVersionService.findAppVersionById(null,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }

    /**
     * 新增APP版本设置数据
     *
     * @param appVersion 
     * @param result
     * @return
     */
    @ApiOperation(value = "保存APP版本设置数据", notes = "保存APP版本设置数据", httpMethod = "POST")
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "保存APP版本设置数据")
    @ResponseBody
    public Result save( @Validated(value = Insert.class) @RequestBody @ApiParam(name = "appVersion ", value = "APP版本设置对象")AppVersion appVersion ,BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            appVersionService.insertAppVersion(appVersion,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
	private String getToken(){		
		return getHttpServletRequest().getHeader("Authorization").substring(7);
    }
}
